package com.jushu.storbox.adapter;

import java.util.ArrayList;
import java.util.List;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public abstract class StanderdTemplateAdapter<T> extends BaseAdapter {

	protected List<T> objectList = null;
	
	public StanderdTemplateAdapter(List<T> translateList) {
		if(translateList == null){
			this.objectList = new ArrayList<T>();
		}else{
			this.objectList = translateList;
		}
	}
	
	@Override
	public int getCount() {
		if(objectList != null) {
			return objectList.size();
		}
		return 0; 
	}

	@Override
	public T getItem(int position) {
		if(objectList != null) {
			return objectList.get(position);
		}
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressWarnings("unchecked")
	@Override
	public final View getView(int position, View contentView, ViewGroup parent) {
		StanderdViewHolder<T> holder;
		if(contentView == null) {
			holder = makeNewViewHolder();
			contentView = holder.inflateFromXML(parent);
			contentView.setTag(holder);
		} else {
			holder = (StanderdViewHolder<T>) contentView.getTag();
		}
		holder.initViewByData(position,getItem(position));
		return holder.getHolderView();
	}

	public List<T> getObjectList() {
		return objectList;
	}
	
	protected abstract StanderdViewHolder<T> makeNewViewHolder();

	public interface StanderdViewHolder<T> {
		public abstract View getHolderView();
		public abstract T getHolderDate();
		public abstract View inflateFromXML(ViewGroup parent);
		public abstract void initViewByData(int position, T data);
	}
}
